package com.darrenchan.leetcode;

import java.util.Stack;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-10-19 22:52
 */
public class Q844 {
    public boolean backspaceCompare(String S, String T) {
        if(S == null || T == null) {
            return S == T;
        }

        Stack<Character> ss = new Stack<>();
        Stack<Character> tt = new Stack<>();

        for (int i = 0; i < S.length(); i++) {
            if(S.charAt(i) != '#') {
                ss.push(S.charAt(i));
            } else {
                if(!ss.isEmpty()) {
                    ss.pop();
                }
            }
        }


        for (int i = 0; i < T.length(); i++) {
            if(T.charAt(i) != '#') {
                tt.push(T.charAt(i));
            } else {
                if(!tt.isEmpty()) {
                    tt.pop();
                }
            }
        }

        if(ss.size() != tt.size()) {
            return false;
        } else {
            while (!ss.isEmpty() && !tt.isEmpty()) {
                if(ss.pop() != tt.pop()) {
                    return false;
                }
            }

            return true;
        }
    }
}
